package com.braintreepayments.cardform.utils;

import java.util.Arrays;

/**
 * A transformation that masks card numbers up to the last 4 digits. For example, it will transform
 * "4111111111111111" to "●●●● 1111". This can be used to mask card numbers in an.
 */
public class CardNumberTransformation {

    private static final String FOUR_DOTS = "••••";

    public String getTransformation(String source) {
        if (source.length() >= 9) {
            StringBuilder result = new StringBuilder()
                    .append(FOUR_DOTS)
                    .append(" ")
                    .append(source.subSequence(source.length() - 4, source.length()));

            char[] padding = new char[source.length() - result.length()];
            Arrays.fill(padding, Character.MIN_VALUE);
            result.insert(0, padding);
            return result.toString();
        }
        return source;
    }
}

